//教练员信息录入

//学员信息录入

import React, { useEffect, useState } from 'react';
import { Layout, Form, Input, Button, Table,Modal, message } from 'antd';
import { useReTip } from '@/hooks/useReTip';
import { fetchUserInitInfo } from '@/store/modules/adminInf';
import { useDispatch, useSelector } from 'react-redux';
import { identity } from 'lodash';

const { Header, Content, Footer } = Layout;

const CoachInformation = () => {
  const dispatch =useDispatch()
  const [form] = Form.useForm();
  const [data, setData] = useState([]);
  const [initData,setInitDta] =useState({})
  const [modalVisible, setModalVisible] = useState(false);
  const {handleRemind} =useReTip()
  const [res,setRes]=useState({})

 const {coachInitInfo} = useSelector(state =>state.adminInf)

  const columns = [
    {
      title: '教练编号',
      dataIndex: 'coachId',
      key: 'coachId',
    },
    {
      title: '姓名',
      dataIndex: 'name',
      key: 'name',
    },
    {
      title: '年龄',
      dataIndex: 'age',
      key: 'age',
    },
    {
      title: '身份证号码',
      dataIndex: 'cardId',
      key: 'cardId',
    },
    {
      title: '手机号',
      dataIndex: 'phone',
      key: 'phone',
    },
    {
      title: '可授课类型',
      dataIndex: 'canTeachType',
      key: 'canTeachType',
    },
    {
      title: '账号密码',
      dataIndex: 'password',
      key: 'password',
    }
  ];

  const handleEdit = (record) => {
    form.setFieldsValue(record);
    setModalVisible(true);
  };

  const handleDelete = (key) => {
    setData(data.filter(item => item.key !== key));
  };
  const handleSave =async () => {
  // const initData ={}
    form.validateFields()
    .then(values =>{
       dispatch( fetchUserInitInfo({
        ...data,
        name:values.name,
        age:values.age,
           cardId:values.cardId,
        phone:values.phone,
        canTeachType:values.canTeachType,
        password:'123456',
        identity:1
      }))
      //  setStudentInfoAPI()
        message.success('新增成功！')
        form.resetFields();//清空内容
        setModalVisible(false);
        // setData(newData)
      })
      .catch(error => console.error(error));

  }
  const handleAddNew = () => {
    const key = Math.random().toString(36).substr(2, 9); // 生成随机key
    const newItem = { key, id: '', name: '', time: '' }; // 创建新数据项
    setData([...data, newItem]); // 添加到数据列表
    form.setFieldsValue(newItem); // 设置表单初始值
    setModalVisible(true); // 打开模态框
  };

  return (
    <Layout>
      <Header style={{ color: 'white' }}>教练账号管理</Header>
      <Content style={{ padding: '10 50px' }}>
        <div style={{ background: '#fff', padding: 24, minHeight: 280 }}>
        <Button onClick={handleAddNew} style={{ marginBottom: 16 }}>
            新增
          </Button>
          <Table columns={columns} dataSource={coachInitInfo} />
        </div>
        <Modal
          title="编辑账号信息"
          visible={modalVisible}
          cancelText='取消'
          okText='提交'
          onCancel={() => setModalVisible(false)}
          onOk={handleSave}
        >
          <Form form={form} layout="vertical" initialValues={{}}>
            <Form.Item name="key" hidden><Input /></Form.Item>
            <Form.Item label="姓名" name="name" rules={[{ required: true, message: '请输入姓名' }]}>
              <Input />
            </Form.Item>
            <Form.Item label="年龄" name="age" rules={[{ required: true, message: '请输入年龄' }]}>
              <Input />
            </Form.Item>
            <Form.Item label="手机号" name="phone" rules={[{ required: true, message: '请输入手机号' }]}>
              <Input />
            </Form.Item>
            <Form.Item label="身份证号码" name="cardId" rules={[{ required: true, message: '请输入身份证号码' }]}>
              <Input />
            </Form.Item>
            <Form.Item label="可授课类型" name="canTeachType" rules={[{ required: true, message: '请输入可授课类型' }]}>
              <Input />
            </Form.Item>
          </Form>
        </Modal>
      </Content>
      <Footer>
     <div style={{position:'absolute',left:'50%'}}>驾校管理系统&copy;2024-2099</div>
   </Footer>
    </Layout>
  );
};

export default CoachInformation;
